package com.unknow.first.app.version.conntroller.app;

import com.unknow.first.app.version.service.AppVersionService;
import com.unknow.first.app.version.vo.AppVersionResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.Map;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.cloud.context.RequestContextManager;
import org.cloud.entity.LoginUserDetails;
import org.cloud.vo.CommonApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "AppVersionController", tags = "APP-版本管理(app)")
@RestController
@RequestMapping("/app/version")
@RequiredArgsConstructor(onConstructor = @__({@Autowired}))
public class AppVersionController {

    private final AppVersionService appVersionService;

    @ApiOperation("是否是最新版本（是无返回，否返回最新版本）")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "app类型(1 android；2 ios)", name = "type", required = true, dataType = "int", paramType = "query"),
        @ApiImplicitParam(value = "版本号", name = "version", required = true, dataType = "String", example = "v1.0.2", paramType = "query")})
    @RequestMapping(method = RequestMethod.GET, value = "/isLatestVersion")
    public CommonApiResult<AppVersionResultVO> isLatestVersion(
        @Min(value = 1, message = "类型不合法") @Max(value = 2, message = "类型不合法") int type,
        @NotBlank(message = "版本号不能为空") String version, @RequestParam("appName") String appName) {
        LoginUserDetails loginUser = RequestContextManager.single().getRequestContext().getUser();
        AppVersionResultVO newVersion = appVersionService.isNewVersion(version, type, loginUser == null ? null : loginUser.getId(),
            appName);
        return CommonApiResult.createSuccessResult(newVersion);
    }

    @ApiOperation("获取最新的安卓和ios版本信息")
    @RequestMapping(method = RequestMethod.GET, value = "/getAndroidAndIos")
    public CommonApiResult<Map<String, AppVersionResultVO>> getAndroidAndIos(@RequestParam("appName") String appName) {
        return CommonApiResult.createSuccessResult(appVersionService.getAndroidAndIos(appName));
    }
}
